package com.example.myappliu10122;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private EditText et_sj;
    private TextView  tv_sj;
    private Button  btn_hq,btn_star,btn_tz;
    private int t;
    private CountDownTimer cdt;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();


    }

    private void setListener() {
        btn_hq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=et_sj.getText().toString();
                tv_sj.setText(str);
                t=Integer.parseInt(str);
                countDown();
            }
        });
        btn_star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                countDown();


                cdt.start();
            }
        });
        btn_tz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cdt.cancel();
            }
        });

    }

    private void countDown() {
        if (cdt !=null){
            cdt.cancel();

        }


        cdt = new CountDownTimer((t+1)*1000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                int k =(int)millisUntilFinished/1000;
                t=k>=t ? t:k;

                tv_sj.setText(t+"");


            }

            @Override
            public void onFinish() {

            }
        };
    }

    private void initView() {
        btn_hq=findViewById(R.id.btn_hq);//获取倒计时
        btn_star=findViewById(R.id.btn_star);//开始
        btn_tz=findViewById(R.id.btn_tz);//停止
        tv_sj=findViewById(R.id.tv_sj);//获取显示时间
        et_sj=findViewById(R.id.et_sj);//输入时间数字



    }
}
